package org.soft.xinyu.equipmentmanagement.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.soft.xinyu.equipmentmanagement.entity.dto.LoginDto;
import org.soft.xinyu.equipmentmanagement.entity.dto.RegisterDto;
import org.soft.xinyu.equipmentmanagement.result.Result;
import org.soft.xinyu.equipmentmanagement.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: xinyu
 * @Date: 2022/01/13/20:22
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

//    @RequestMapping("/login")
//    public Result login(String username, String password) {
//        String result = "{\"token\":\"admin-token\"}";
//        JSONObject jsonResult = JSON.parseObject(result);
//        return Result.success(20000, "", jsonResult, 0);

    @PostMapping("/login")
    @ResponseBody
    public Result userLogin(@RequestBody LoginDto user) throws Exception {
        try{
            log.info("Login successful");
            Result result = userService.userLogin(user);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            log.info("Login failure");
            return Result.fail("User login fail");
        }
    }

    @PostMapping("/register")
    @ResponseBody
    public Result userRegister(@RequestBody RegisterDto registerDto) throws Exception {
        try{
            Result result = userService.userRegister(registerDto);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("system error");
        }
    }

//    @PostMapping("/hello")
//    @ResponseBody
//    public Result hello(@RequestBody testDto test) {
//        System.out.println(test.getTest());
//        return new ResultInfo().success(2000, test.getTest());
//    }

//    @GetMapping("/logout")
//    @ResponseBody
//    public Result userLogout() {
//        try {
//            log.info("User logout");
//            usersService.userLogout();
//            return new Result.success("User logout success");
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result.fail("User logout fail");
//        }
//    }

    @RequestMapping("/info")
    public Result userInfo() {
        String result = "{\"roles\":[\"admin\"],\"introduction\":\"I am a super administrator\",\"avatar\":\"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif\",\"name\":\"超级管理员\"}";
        JSONObject jsonResult = JSON.parseObject(result);
        return Result.success(20000, "", jsonResult, 0);
    }

//    @RequestMapping("/logout")
//    public Result logout() {
//        return Result.success(20000, "", new String("success"), 0);
//    }
}
